package com.sx.service.impl;

import com.sx.dao.RoleMapper;
import com.sx.domain.Role;
import com.sx.domain.RoleMenuVo;
import com.sx.domain.Role_menu_relation;
import com.sx.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public List<Role> findAllRole(Role role) {
        List<Role> allRole = roleMapper.findAll(role);
        return allRole;
    }

    @Override
    public List<String> findMenuByRoleId(Integer roleId) {
        List<String> list = roleMapper.findMenuByRoleId(roleId);
        return list;
    }

    @Override
    public void RoleContextMenu(RoleMenuVo roleMenuVo) {
        //清空中间表关系
        roleMapper.deleteRoleContextMenu(roleMenuVo.getRoleId());
        for (Integer integer : roleMenuVo.getMenuId()) {
            Role_menu_relation roleMenuRelation = new Role_menu_relation();
            roleMenuRelation.setRoleId(roleMenuVo.getRoleId());
            roleMenuRelation.setMenuId(integer);
            roleMenuRelation.setCreatedTime(new Date());
            roleMenuRelation.setUpdatedTime(new Date());
            roleMenuRelation.setCreatedBy("system");
            roleMenuRelation.setUpdatedby("system");
            roleMapper.RoleContextMenu(roleMenuRelation);
        }

    }

    @Override
    public void deleteRole(Integer id) {
        // 清空中间表
        roleMapper.deleteRoleContextMenu(id);
        roleMapper.deleteRole(id);
    }

}
